<?php
/**
 * school.php
 *
 * $Id: school_extend.php 177 2012-02-25 03:53:40Z alpha1130@gmail.com $
 */
 
class School_extend extends OA_Controller
{
	const CATAGORY = OA_CONST_CATAGORY::MARKETING;

	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * @name 校园推广
	 * @desc 校园推广首页
	 *
	 */
	public function index()
	{
		
	}
	
	/**
	 * @name 学校信息
	 * @desc 校园推广学校信息列表
	 *
	 */
	public function school_list()
	{
		// Get schools
		$schools = array();
		$this->load->model('School_model', 'school');
		foreach($this->school->get_list() as $row)
		{
			$schools[$row['area_id']][] = $row;
		}
		
		// Get area
		$areas = array();
		$query = $this->db->from('area')->get();
		foreach($query->result_array() as $row)
		{
			$areas[$row['id']] = $row;
		}
		
		$this->load->view('school_extend_school_list', array(
            'schools' => $schools,
            'areas' => $areas,
		));
	}
	
	/**
	 * @name 学校添加
	 * @desc 学校添加表单页面
	 *
	 */
	public function school_add_page()
	{
		$this->load->view('school_extend_school_add', array(
            'return_url' => $_SERVER['HTTP_REFERER'],
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	/**
	 * @name 学校添加
	 * @desc 学校添加数据提交
	 * @method post
	 */
	public function school_add()
	{
		list($safe_vars) = $this->check(array(
            'area_id' => FC_int('区县')->required(),
            'name' => FC_chs('学校名')->min(2)->max(5)->required(),
            'return_url' => FC_str('返回地址', '')->max(255),
		));
		
		$this->db->insert('school', array(
            'name' => $safe_vars['name'],
            'area_id' => $safe_vars['area_id'],
		));
		
		$this->load->view('info', array(
            'message' => '添加成功',
            'url' => $safe_vars['return_url'],
		));
	}
	
	/**
	 * @name 学校编辑
	 * @desc 学校编辑表单页面
	 *
	 */
	public function school_edit_page()
	{
		list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
		));
		
		$this->load->model('School_model', 'school');
		$school = $this->school->get_by_id($safe_vars['school_id']);
		
		if( ! $school)
		{
			show_error('很抱歉，你编辑的学校不存在');
		}
		
		$last_extend = $this->school->get_last_extend_time_by_id($safe_vars['school_id']);
		
		$this->load->view('school_extend_school_edit', array(
			'last_extend' => $last_extend,
            'school' => $school,
            'school_id' => $safe_vars['school_id'],
            'return_url' => $_SERVER['HTTP_REFERER'],
            'token_name' => $this->security->get_csrf_token_name(),
            'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	/**
	 * @name 学校编辑
	 * @desc 学校编辑数据提交
	 * @method post
	 *
	 */
	public function school_edit()
	{
		list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required(),
            'name' => FC_chs('学校名')->min(2)->max(5)->required(),
            'grade1_class_num' => FC_int('高一班级数', 0)->max(99999),
            'grade2_class_num' => FC_int('高二班级数', 0)->max(99999),
			'grade3_class_num' => FC_int('高三班级数', 0)->max(99999),
            'grade1_people_num' => FC_int('高一人数', 0)->max(99999),
            'grade2_people_num' => FC_int('高二人数', 0)->max(99999),
			'grade3_people_num' => FC_int('高三人数', 0)->max(99999),
			'location' => FC_str('地址')->max(45),
			'web' => FC_str('网址', '')->max(100),
			'tel_jwc' => FC_str('教务处电话')->max(45),
			'tel_zs' => FC_str('招生老师电话')->max(45),
			'tel_zp' => FC_str('招聘老师电话')->max(45),
			'mid_rest_time' => FC_str('午休时间段')->max(45),
			'at_rest_time' => FC_str('晚休时间段')->max(45),
			'mid_rest_lock' => FC_str('午休是否锁门')->max(45),
			'teacher_info' => FC_str('校内教师信息')->max(45),
			'return_url' => FC_str('返回地址', '')->max(255)
		));
		
		$this->load->model('School_model', 'school');
		$school = $this->school->get_by_id($safe_vars['school_id']);
		if( ! $school)
		{
			show_error('很抱歉，你要编辑的学校不存在');
		}
		
		// update school
		$this->db->where('id', $school['id'])->update('school', array(
            'name' => $safe_vars['name'],
			'grade1_class_num' => $safe_vars['grade1_class_num'],
            'grade2_class_num' => $safe_vars['grade2_class_num'],
			'grade3_class_num' => $safe_vars['grade3_class_num'],
			'grade1_people_num' => $safe_vars['grade1_people_num'],
            'grade2_people_num' => $safe_vars['grade2_people_num'],
			'grade3_people_num' => $safe_vars['grade3_people_num'],
			'location' => $safe_vars['location'],
			'web' => $safe_vars['web'],
			'tel_jwc' => $safe_vars['tel_jwc'],
			'tel_zs' => $safe_vars['tel_zs'],
			'tel_zp' => $safe_vars['tel_zp'],
			'mid_rest_time' => $safe_vars['mid_rest_time'],
			'at_rest_time' => $safe_vars['at_rest_time'],
			'mid_rest_lock' => $safe_vars['mid_rest_lock'],
			'teacher_info' => $safe_vars['teacher_info'],
		));
		
		// 
		$this->load->view('info', array(
            'message' => '修改成功',
            'url' => $safe_vars['return_url'],
		));
	}
	
	/**
	 * @name 学校删除
	 * @desc 学校信息删除
	 * @method post
	 *
	 */
	public function school_delete()
	{
		list($safe_vars) = $this->check(array(
            'school_id' => FC_int('学校')->required()
        ));
		
		$this->db->update('school', 
			array('flag' => OA_CONST_SCHOOL::FLAG_DELETE), 
			array('id' => $safe_vars['school_id']));
		
		$this->ajax_out();
	}
	
	/**
	 * @name 项目管理列表
	 * @desc 校园推广项目管理列表
	 *
	 */
	public function project_list()
	{
		list($safe_vars) = $this->check(array(
			'page' => FC_int('页码', '1')->min(1)
		));
		
		$projects = $members = $schools = $pagination = array();
		$projects_count = $this->db
			->where('flag', OA_CONST_PROJECT::FLAG_NORMAL)
			->count_all_results('project');
		
		if($projects_count)
		{
			$this->load->helper('page');
				$pagination = oa_page(
				$safe_vars['page'],
				$projects_count,
				$this->config->item('per_page')
			);
			
			// get projects
			$projects = $this->db
				->from('project')
				->where('flag', OA_CONST_PROJECT::FLAG_NORMAL)
				->order_by('start_time', 'desc')
				->limit($this->config->item('per_page'), $pagination['record_offset'])
				->get()->result_array();
			
			$projects_id = array();
			
			foreach($projects as $project)
			{
				$projects_id[] = $project['id'];
			}
			
			// get members in projects
			$member_array = $this->db
				->select('member.name, p_id')
				->from('project_member')
				->join('member', 'member.id=project_member.m_id', 'left')
				->where_in('p_id', $projects_id)
				->get()
				->result_array();
			foreach($member_array as $member)
			{
				$members[$member['p_id']][] = $member['name'];
			}
			array_walk($members, function(&$item, $key){
				$item = join(',', $item);
			});
			
			// get schools in projects
			$school_array = $this->db
				->select('school.name, p_id')
				->from('project_school')
				->join('school', 'school.id=project_school.s_id', 'left')
				->where('school.flag', OA_CONST_SCHOOL::FLAG_NORMAL)
				->where_in('p_id', $projects_id)
				->get()->result_array();
			foreach($school_array as $school)
			{
				$schools[$school['p_id']][] = $school['name'];
			}
			array_walk($schools, function(&$item, $key){
				$item = join(',', $item);
			});
			
		}
		
		$this->load->view('school_extend_project_list', array(
            'projects' => $projects,
			'members' => $members,
			'schools' => $schools,
			'pagination' => $pagination
		));
	}
	
	/**
	 * @name 项目添加
	 * @desc 项目添加表单页面
	 */
	public function project_add_page()
	{
		// Get schools
		$schools = array();
		$query = $this->db
			->where('flag', OA_CONST_SCHOOL::FLAG_NORMAL)
			->get('school');
		foreach($query->result_array() as $row)
		{
			$schools[$row['area_id']][] = $row;
		}
		
		// Get area
		$areas = array();
		$query = $this->db->from('area')->get();
		foreach($query->result_array() as $row)
		{
			$areas[$row['id']] = $row;
		}
		
		// Get members
		$members = array();
		$query = $this->db
			->order_by('status')
			->get('member');
		foreach($query->result_array() as $row)
		{
			$members[$row['status']][] = $row;
		}
		
		$this->load->view('school_extend_project_add', array(
			'return_url' => $_SERVER['HTTP_REFERER'],
			'schools' => $schools,
			'members' => $members,
			'areas' => $areas,
			'member_status_label' => OA_CONST_MEMBER::$STATUS,
			'token_name' => $this->security->get_csrf_token_name(),
		    'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	/**
	 * @name 项目添加
	 * @desc 项目添加数据提交
	 * @method post
	 */
	public function project_add()
	{
		list($safe_vars) = $this->check(array(
			'start_time' => FC_date('项目起始时间')->required(),
			'end_time' => FC_date('项目结束时间')->required(),
			'name' => FC_str('项目名称')->min(1)->max(10)->required(),
			'school' => FC_int('项目学校')->multi()->min(1),
			'member' => FC_int('项目成员')->multi()->min(1),
			'return_url' => FC_str('返回地址', '')->max(255),
		));
		
		$this->load->library('upload', $this->config->item('upload'));
		if($this->upload->do_upload('attachment'))
		{
			$data = $this->upload->data();
			$safe_vars['attach_path'] = $data['file_name'];
			$safe_vars['attach_name'] = $data['orig_name'];
		}
		else
		{
			$safe_vars['attach_path'] = $safe_vars['attach_name'] = '';
		}
		
		$this->db->trans_start();
		
		// insert project
		$this->db->insert('project', array(
			'start_time' => strtotime($safe_vars['start_time']),
			'end_time' => strtotime($safe_vars['end_time']),
			'name' => $safe_vars['name'],
			'attach_path' => $safe_vars['attach_path'],
			'attach_name' => $safe_vars['attach_name'],
		));
		$p_id = $this->db->insert_id();
		
		// insert member
		if($safe_vars['member'])
		{
			$insert_members = array();
			foreach($safe_vars['member'] as $member)
			{
				$insert_members[] = array('p_id' => $p_id, 'm_id' => $member);
			}
			$this->db->insert_batch('project_member', $insert_members);
		}
		
		// insert school
		if($safe_vars['school'])
		{
			$insert_schools = array();
			foreach($safe_vars['school'] as $school)
			{
				$insert_schools[] = array('p_id' => $p_id, 's_id' => $school);
			}
			$this->db->insert_batch('project_school', $insert_schools);
		}
		
		// insert feedback
		if($safe_vars['member'] && $safe_vars['school'])
		{
			$insert_feedbacks = array();
			foreach($safe_vars['school'] as $s_id)
			{
				foreach($safe_vars['member'] as $m_id)
				{
					$insert_feedbacks[] = array(
						'p_id' => $p_id,
						'm_id' => $m_id,
						's_id' => $s_id
					);
				}
			}
			$this->db->insert_batch('project_feedback', $insert_feedbacks);
		}
		
		$this->db->trans_complete();
		
		$this->load->view('info', array(
            'message' => '添加成功',
            'url' => $safe_vars['return_url'],
		));
	}
	
	/**
	 * @name 项目明细
	 * @desc 项目明细
	 *
	 */
	public function project_detail()
	{
		list($safe_vars) = $this->check(array(
			'id' => FC_int('项目')->min(1)->required()
		));
		
		// get project
		$this->load->model('Project_model', 'project');
		$project = $this->project->get_by_id($safe_vars['id']);
		
		if( ! $project)
		{
			show_error('很抱歉，你查看的项目不存在');
		}
		
		$this->load->model('Member_model', 'member');
		$this->load->model('School_model', 'school');
		$this->load->model('Feedback_model', 'feedback');
		
		// get member
		$members = $this->member->get_map_by_project($project['id']);
		
		// get school
		$schools = $this->school->get_map_by_project($project['id']);
		
		// get feedback list with school catalog
		$feedbacks['school'] 
			= $this->feedback->get_school_merge_list(array($project['id']));
		if($feedbacks['school'])
		{
			$feedbacks['school'] = $feedbacks['school'][$project['id']];
		}
		
		// get feedback list with member catalog
		$feedbacks['member']
			= $this->feedback->get_member_merge_list(array($project['id']));
		if($feedbacks['member'])
		{
			$feedbacks['member'] = $feedbacks['member'][$project['id']];
		}
		
		$this->load->view('school_extend_project_detail', array(
			'project' => $project,
			'members' => $members,
			'schools' => $schools,
			'feedbacks' => $feedbacks,
			'enter_status' => OA_CONST_FEEDBACK::$ENTER,
            'token_name' => $this->security->get_csrf_token_name(),
		    'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	public function project_attach_get()
	{
		list($safe_vars) = $this->check(array(
			'id' => FC_int('项目')->min(1)->required()
		));
		
		// get project
		$project = $this->db->from('project')
			->where('id', $safe_vars['id'])
			->where('flag', OA_CONST_PROJECT::FLAG_NORMAL)
			->get()
			->row_array();
		
		if( ! $project)
		{
			show_error('很抱歉，你下载的附件不存在');
		}
		
		$upload_config = $this->config->item('upload');
		$attach_name = $project['attach_name'];
		$attach_path = $upload_config['upload_path'].'/'.$project['attach_path'];
		$attach_size = filesize($attach_path);
		
		if( ! file_exists($attach_path))
		{
			show_error('很抱歉，你下载的附件不存在');
		}
		
		// output download header
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: private", false);
		header('Content-Type: application/force-download');
		header("Content-Disposition: attachment; filename=\"{$attach_name}\";");
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".$attach_size);
		readfile($attach_path);
		
	}
	
	/**
	 * @name 项目删除
	 * @desc 项目信息删除
	 * @method post
	 *
	 */
	public function project_delete()
	{
		list($safe_vars) = $this->check(array(
            'project_id' => FC_int('项目')->required()
        ));
		
		$this->db->update('project', 
			array('flag' => OA_CONST_PROJECT::FLAG_DELETE), 
			array('id' => $safe_vars['project_id']));
		
		$this->ajax_out();
	}
	
	/**
	 * @name 成员列表
	 * @desc 成员列表
	 *
	 */
	public function member_list()
	{
		$members = array();
		$query = $this->db->order_by('status')->get('member');
		foreach($query->result_array() as $row)
		{
			$members[$row['status']][] = $row;
		}
		
		$this->load->view('school_extend_member_list', array(
			'member_status_label' => OA_CONST_MEMBER::$STATUS,
			'members' => $members
		));
	}
	
	/**
	 * @name 成员添加
	 * @desc 成员添加表单
	 *
	 */
	public function member_add_page()
	{
		$this->load->view('school_extend_member_add', array(
			'token_name' => $this->security->get_csrf_token_name(),
		    'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	/**
	 * @name 成员添加
	 * @desc 成员添加数据提交
	 * @method post
	 *
	 */
	public function member_add()
	{
		list($safe_vars) = $this->check(array(
			'name' => FC_str('成员姓名')->min(1)->max(4)->required(),
			'status' => FC_int('成员类别')->required(),
		));
		
		$this->db->insert('member', array(
			'name' => $safe_vars['name'],
			'status' => $safe_vars['status']
		));
		$member_id = $this->db->insert_id();
		
		$this->load->view('info', array(
            'message' => '添加成功',
            'url' => '/market/se/member/detail?member_id='.$member_id,
		));
	}
	
	/**
	 * @name 成员明细
	 * @desc 成员明细
	 *
	 */
	public function member_detail()
	{
		list($safe_vars) = $this->check(array(
			'member_id' => FC_int('成员')->required()
		));
		
		$member = $this->db
			->where('id', $safe_vars['member_id'])
			->get('member')->row_array();
		
		if( ! $member)
		{
			show_error('对不起，你查看的成员不存在');
		}
		
		$this->load->view('school_extend_member_detail', array(
			'member' => $member,
			'token_name' => $this->security->get_csrf_token_name(),
		    'token_hash' => $this->security->get_csrf_hash(),
		));
	}
	
	/**
	 * @name 成员状态设置
	 * @desc 成员状态设置
	 * @method post
	 */
	public function member_set()
	{
		list($safe_vars) = $this->check(array(
			'member_id' => FC_int('成员')->required(),
			'member_status' => FC_int('成员类型')->required()
		));
		
		$member = $this->db
			->where('id', $safe_vars['member_id'])
			->get('member')->row_array();
		
		if( ! $member)
		{
			show_error('对不起，你查看的成员不存在');
		}
		
		$this->db->update('member', 
			array('status' => $safe_vars['member_status']),
			array('id' => $safe_vars['member_id']));
		$this->ajax_out();
	}
	
	/**
	 * @name 队长反馈列表
	 * @desc 队长反馈列表
	 *
	 */
	public function feedback_list()
	{
		
	}
	
	/**
	 * @name 队长反馈明细
	 * @desc 队长反馈明细
	 */
	public function feedback_detail()
	{
		
	}
	
	/**
	 * @name 队长反馈编辑
	 * @desc 队长反馈编辑表单
	 */
	public function feedback_edit_page()
	{
		
	}
	
	/**
	 * @name 队长反馈编辑
	 * @desc 队长反馈编辑数据提交
	 *
	 */
	public function feedback_edit()
	{
		
	}
	
}

/* End file of school.php */
/* Location: school.php */